/*
  ivl_sub.c  -  implementation of subtraction functions

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki

  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

int ivl_sub(ivl_ptr ivl, ivl_srcptr x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x) || DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub(IVL_LEFT(ivl), IVL_LEFT(x), IVL_RIGHT(y), GMP_RNDD);
		inexact_right = mpfr_sub(IVL_RIGHT(ivl), IVL_RIGHT(x), IVL_LEFT(y), GMP_RNDU);
	
		IVL_CHECK(ivl, x->domain | y->domain, inexact, inexact_left, inexact_right);				
	}
	
	return inexact;	
}

int ivl_sub_u(ivl_ptr ivl, ivl_srcptr x, unsigned long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub_ui(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_sub_ui(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
	
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);				
	}
	
	return inexact;		
}

int ivl_sub_s(ivl_ptr ivl, ivl_srcptr x, long y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub_si(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_sub_si(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
	
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_sub_d(ivl_ptr ivl, ivl_srcptr x, double y) {
	int inexact = 0, inexact_left, inexact_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		mpfr_init2(tmp, 53);
		
		inexact_left = mpfr_set_d(tmp, y, GMP_RNDD);
		inexact_left |= mpfr_sub(IVL_LEFT(ivl), IVL_LEFT(x), tmp, GMP_RNDD);
		
		inexact_right = mpfr_set_d(tmp, y, GMP_RNDU);
		inexact_right |= mpfr_sub(IVL_RIGHT(ivl), IVL_RIGHT(x), tmp, GMP_RNDU);
		
		mpfr_clear(tmp);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_sub_z(ivl_ptr ivl, ivl_srcptr x, mpz_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub_z(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_sub_z(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_sub_q(ivl_ptr ivl, ivl_srcptr x, mpq_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub_q(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_sub_q(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
		
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_sub_f(ivl_ptr ivl, ivl_srcptr x, mpfr_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(x))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub(IVL_LEFT(ivl), IVL_LEFT(x), y, GMP_RNDD);
		inexact_right = mpfr_sub(IVL_RIGHT(ivl), IVL_RIGHT(x), y, GMP_RNDU);
	
		IVL_CHECK(ivl, x->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_u_sub(ivl_ptr ivl, unsigned long x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_ui_sub(IVL_LEFT(ivl), x, IVL_LEFT(y), GMP_RNDD);
		inexact_right = mpfr_ui_sub(IVL_RIGHT(ivl), x, IVL_RIGHT(y), GMP_RNDU);

		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_s_sub(ivl_ptr ivl, long x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_si_sub(IVL_LEFT(ivl), x, IVL_LEFT(y), GMP_RNDD);
		inexact_right = mpfr_si_sub(IVL_RIGHT(ivl), x, IVL_RIGHT(y), GMP_RNDU);
	
		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_d_sub(ivl_ptr ivl, double x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	mpfr_t tmp;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {		
		mpfr_init2(tmp, 53);
				
		inexact_left = mpfr_set_d(tmp, x, GMP_RNDD);
		inexact_left |= mpfr_sub(IVL_LEFT(ivl), tmp, IVL_LEFT(y), GMP_RNDD);
		
		inexact_right = mpfr_set_d(tmp, x, GMP_RNDU);
		inexact_right |= mpfr_sub(IVL_RIGHT(ivl), tmp, IVL_RIGHT(y), GMP_RNDU);
		
		mpfr_clear(tmp);
		
		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_z_sub(ivl_ptr ivl, mpz_srcptr x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		/* negation is exact but we possibly changed MPFR precision */
		inexact_left = mpfr_neg(IVL_LEFT(ivl), IVL_RIGHT(y), GMP_RNDD);
		inexact_right = mpfr_neg(IVL_LEFT(ivl), IVL_LEFT(y), GMP_RNDU);
		
		inexact_left |= mpfr_add_z(IVL_LEFT(ivl), IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right |= mpfr_add_z(IVL_RIGHT(ivl), IVL_RIGHT(ivl), x, GMP_RNDU);
		
		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_q_sub(ivl_ptr ivl, mpq_srcptr x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		/* negation is exact but we possibly changed MPFR precision */
		inexact_left = mpfr_neg(IVL_LEFT(ivl), IVL_RIGHT(y), GMP_RNDD);
		inexact_right = mpfr_neg(IVL_LEFT(ivl), IVL_LEFT(y), GMP_RNDU);
		
		inexact_left |= mpfr_add_q(IVL_LEFT(ivl), IVL_LEFT(ivl), x, GMP_RNDD);
		inexact_right |= mpfr_add_q(IVL_RIGHT(ivl), IVL_RIGHT(ivl), x, GMP_RNDU);
		
		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

int ivl_f_sub(ivl_ptr ivl, mpfr_srcptr x, ivl_srcptr y) {
	int inexact = 0, inexact_left, inexact_right;
	
	if (DOM_UNDEFINED(y))
		ivl->domain = IVL_UNDEFINED;
	else {
		inexact_left = mpfr_sub(IVL_LEFT(ivl), x, IVL_LEFT(y), GMP_RNDD);
		inexact_right = mpfr_sub(IVL_RIGHT(ivl), x, IVL_RIGHT(y), GMP_RNDU);
	
		IVL_CHECK(ivl, y->domain, inexact, inexact_left, inexact_right);
	}
	
	return inexact;
}

